package com.commander;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.*;

import com.commander.gui.CommanderGUIHandler;
import com.commander.gui.messages.*;
import com.commander.structures.CommanderProcess;
import java.sql.*;

/**
 * Commander.java
 * @author Kelly Grogan
 *
 *TODO figure out what this class will do
 */
public class Commander {
	
	/**
	 * The one and only Commander instance
	 */
	public static Commander theCommander = new Commander();
	
	private String username, password;
	
	/**
	 * A Stack data structure for data handling
	 */
	private Stack<CommanderProcess> stack;
	
	/**
	 * A Queue data structure for data handling
	 */
	private Queue<CommanderProcess> queue;
	
	/**
	 * A Connection to a MySQL DB
	 */
	private Connection connection;
	
	private final String baseURL = "jdbc:mysql://localhost:3306";
	
	/**
	 * A private constructor so this is a true singleton instance.
	 * It initializes all of its resources.
	 */
	private Commander(){
		initialize();
	}
	
	/**
	 * Starts the Commander's processes
	 */
	public void run(){
		CommanderGUIHandler.initialize();
		//CommanderGUIHandler.getShell().pack();
		CommanderGUIHandler.getShell().open();
		while(!CommanderGUIHandler.getShell().isDisposed())
			if(!CommanderGUIHandler.getDisplay().readAndDispatch())
				CommanderGUIHandler.getDisplay().sleep();
		CommanderGUIHandler.getDisplay().dispose();
	}
	
	/**
	 * Initializes all of the Commander's resources
	 */
	private void initialize(){
		stack = new Stack<CommanderProcess>();
		queue = new LinkedList<CommanderProcess>();
	}
	
	public void addProcess(CommanderProcess process){
		queue.add(process);
	}
	
	public void removeProcess(CommanderProcess process){
		queue.remove(process);
	}
	
	/**
	 * A static factory method to return the one and only Commander
	 * @return theCommander The one and only Commander instance
	 */
	public static Commander getCommander(){return theCommander;}

	public void exit() {
		System.exit(0);		
	}

	public void login(String username, String password) {
		this.username = username;
		this.password = password;
		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			connection = DriverManager.getConnection(baseURL, username, password);
		}
		catch(Exception e){
			new ErrorMessage(e.getMessage());
		}
	}

	public Connection getConnection() {
		return connection;
	}
	
	public void connectToDatabase(String database){
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			connection = DriverManager.getConnection(baseURL+"/"+database, username, password);
			System.out.println(connection.getCatalog());
		} catch (Exception e) {
			new ErrorMessage(e.getMessage());
		}
	}
	public String[] getAvailableDBs() {
		try {
			DatabaseMetaData dbmd = connection.getMetaData();
			//String name = dbmd.g
			List<String> list = new LinkedList<String>();
			Statement cs = connection.createStatement();
			ResultSet sqls = dbmd.getCatalogs();//cs.executeQuery("SHOW databases");
			sqls.beforeFirst();
			while(sqls.next()){
				list.add(sqls.getString(1));
			}
			String[] data = new String[list.size()];
			list.toArray(data);
			return data;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// TODO Auto-generated method stub
		return null;
	}
	
	public void setDatabase(String db){
//		Statement cs;
//		try {
//			cs = connection.createStatement();
//			cs.executeQuery("USE "+db);
//			DatabaseMetaData dbmd = connection.getMetaData();
//			System.out.println("Current DB:  "+connecti);
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
	
	public static void main(String[] args){
		Commander.getCommander().login("root", "root");
		String[] s = Commander.getCommander().getAvailableDBs();
		for(String str : s){
			System.out.println(str);
		}
		Commander.getCommander().connectToDatabase("registrations");
	}
}
